package edu.nf.ch01.common.config;

import edu.nf.ch01.web.filter.MyFiler;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author wangl
 * @date 2025/4/16
 * MVC配置类
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Bean
    public FilterRegistrationBean<MyFiler> myFilter() {
        FilterRegistrationBean<MyFiler> filterRegistrationBean = new FilterRegistrationBean<>();
        //装配过滤器
        filterRegistrationBean.setFilter(new MyFiler());
        //设置过滤器名称
        filterRegistrationBean.setName("myFilter");
        //映射请求地址
        filterRegistrationBean.addUrlPatterns("/*");
        //过滤器优先级别(数值越小优先级别越高)
        filterRegistrationBean.setOrder(0);
        return filterRegistrationBean;
    }

    /**
     * 全局跨域配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*");
    }
}